import {Component, OnInit, ViewChild} from '@angular/core';
import {NzMessageService} from 'ng-zorro-antd';
import {PageEntity} from '../../../core/domain/page.entity';
import {OtherUtil} from '../../../utils/other.util';
import {StatusEnum} from '../../../core/enum/status.enum';
import {PaginationUtils} from '../../../utils/paginationUtils';
import {FilterEnum, FilterTypeEnum} from '../../../core/enum/filter.enum';
import {DataDealService} from './data-deal.service';
import {HttpClient} from '@angular/common/http';

@Component({
  selector: 'app-data-deal',
  templateUrl: './data-deal.component.html',
  styleUrls: ['./data-deal.component.scss'],
  providers: [DataDealService]
})
export class DataDealComponent implements OnInit {
  pageInfo: PageEntity = new PageEntity();
  dataList = [];
  mapOfCheckedId = {};
  isAllCheck = false;
  isIndeterminate = false;
  canDelete = true;
  fileNameSearch = null;

  isVisible = false;
  fileName = null;
  remark = null;
  constructor(
    private http: DataDealService,
    private message: NzMessageService,
    private httpClient: HttpClient,
  ) { }

  ngOnInit() {
    this.page(this.pageInfo);
  }

  page(page) {
    this.http.page(page).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.dataList = res.datas.data;
        this.pageInfo.totalRecords = res.datas.totalRecords;
      }
    });
  }

  /**
   * 点击删除
   */
  deleteAll() {
    const arr = [];
    for (const val in this.mapOfCheckedId) {
      if (this.mapOfCheckedId[val]) {
        arr[arr.length] = val;
      }
    }
    if (arr.length < 1) {
      this.message.warning('请勾选要删除的数据!');
      return;
    }
    this.http.deleteAll(arr).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.cancelFn();
        this.page(this.pageInfo);
      }
    });
  }

  clickDelete(id) {
    this.http.delete(id).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.page(this.pageInfo);
      }
    });
  }

  /**
   * 点击查询
   */
  clickSearch() {
    if (this.fileNameSearch) {
      this.pageInfo.filters['fileName'.toString()] = PaginationUtils.filters(this.fileNameSearch, FilterEnum.CONTAINS);
    } else {
      delete this.pageInfo.filters['fileName'.toString()];
    }
    this.pageInfo.first = 1;
    this.page(this.pageInfo);
  }

  /**
   * 点击重置
   */
  resetSearch() {
    this.fileNameSearch = null;
    this.pageInfo = new PageEntity();
    this.page(this.pageInfo);
  }

  /**
   * 改变第几页
   */
  changePageIndex(index: number) {
    this.pageInfo.first = index;
    this.page(this.pageInfo);
  }

  /**
   * 改变每页几条
   */
  changePageSize(size: number) {
    this.pageInfo.first = 1;
    this.pageInfo.rows = size;
    this.page(this.pageInfo);
  }

  checkAll(value: boolean): void {
    this.dataList.forEach(item => (this.mapOfCheckedId[item.id] = value));
    this.refreshStatus();
  }

  refreshStatus(): void {
    this.isAllCheck = this.dataList.every(item => this.mapOfCheckedId[item.id]);
    this.isIndeterminate =
      this.dataList.some(item => this.mapOfCheckedId[item.id]) &&
      !this.isAllCheck;
    this.checkDelete();
  }

  checkDelete(): void {
    this.canDelete = OtherUtil.deleteIds(this.mapOfCheckedId).length <= 0;
  }

  cancelFn(): void {
    this.canDelete = true;
    this.isAllCheck = false;
    this.mapOfCheckedId = {};
    this.isIndeterminate = false;
  }

  // 数据备份
  dataBf() {
    this.isVisible = true;
    this.fileName = null;
    this.remark = null;
  }

  // 数据备份保存
  submit() {
    this.http.createData({
      fileName: this.fileName,
      content: this.remark
    }).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.isVisible = false;
        this.page(this.pageInfo);
      }
    });
  }

  // 数据恢复
  backdata(data) {
    this.http.rollbackData({id: data.id}).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.page(this.pageInfo);
      }
    });
  }

  aicgo() {
    this.http.syncDataToTransfer().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
      }
    });
  }

  middlego() {
    this.http.syncTransferToLocal().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
      }
    });
  }
}

